package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.exception.CustomException;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    DishMapper mapper;
    @Autowired
    SetmealService setmealService;
    @Autowired
    SetmealDishService setmealDishService;

    // 涉及多表的操作需要添加事务
    @Override
    @Transactional
    public void saveForFlavor(DishDto dishDto) {

        // 添加正常的菜品数据
        save(dishDto);
        // 添加的口味是个集合,提取出来
        List<DishFlavor> flavorList = dishDto.getFlavors();
        // 在菜品口味表中设置添加菜品id
        flavorList.forEach(dishFlavor -> dishFlavor.setDishId(dishDto.getId()));
        // 在菜品口味的表中添加口味数据和菜品的id
        dishFlavorService.saveBatch(flavorList);

    }

    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        // 先改正常的dish数据
        updateById(dishDto);
        Long dishId = dishDto.getId();
        // 把口味里的原有数据清除 delete from 口味表 where dishId = id
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, dishId);
        dishFlavorService.remove(lqw);
        // 把修改后的口味数据存入
        // 添加的口味是个集合,提取出来
        List<DishFlavor> flavors = dishDto.getFlavors();
        // 在菜品口味表中设置添加菜品id
        flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishId));
        // 在菜品口味的表中添加更新的数据
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public DishDto getByIdWithFlavor(Long id) {
        DishDto dishDto = new DishDto();
        // 先查正经的dish数据
        Dish dish = getById(id);
        //对象拷贝BeanUtils.copyProperties(a,b);把a放到b里
        BeanUtils.copyProperties(dish,dishDto);
        //  再查dish的口味数据
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, id);
        List<DishFlavor> list = dishFlavorService.list(lqw);
        //  封装入dishDto里
        dishDto.setFlavors(list);
        return dishDto;
    }

    @Override
    public IPage<DishDto> getPageByName(Integer page, Integer pageSize, String name) {
        // 利用xml完成动态sql和多表查询
        // 调用mapper 去查询数据,service只负责做出分页功能即可
        Page<DishDto> pageInfo = new Page<>();
        return mapper.getPageByName(pageInfo,name) ;
    }

    @Override
    public void deleteByIds(Long[] ids) {
        // 先查一下要删除的id是否关联了套餐
        LambdaQueryWrapper<SetmealDish> sdLqw = new LambdaQueryWrapper<>();
        sdLqw.in(SetmealDish::getDishId,ids);
        int sdCount = setmealDishService.count(sdLqw);
        if (sdCount > 0) {
            throw new CustomException("有菜品关联了套餐,不能删除");
        }
        //  再查要删除的id中 是否有起售的状态 status=1的
        LambdaQueryWrapper<Dish> dishLqw = new LambdaQueryWrapper<>();
        dishLqw.in(Dish::getId, ids);
        dishLqw.eq(Dish::getStatus, 1);
        int dishCount = count(dishLqw);
        if (dishCount > 0) {
            throw new CustomException("有菜品正在销售,不能删除");
        }

        // 确认都没有问题了再删
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.in(Dish::getId, ids);
        remove(lqw);

    }

    @Override
    @Transactional
    public void updateStatus(Integer type, Long[] ids) {

/*        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.in(Dish::getId,ids);
        List<Dish> list =list(lqw);
        List<Dish> dishs = new ArrayList<>();
        if (type == 1) {
            lqw.eq(Dish::getStatus,1);
            dishs = list.stream().map(dish -> {
                dish.setStatus(0);
                return dish;
            }).collect(Collectors.toList());

        } else if (type == 0) {
            lqw.eq(Dish::getStatus,0);
            dishs = list.stream().map(dish -> {
                dish.setStatus(1);
                return dish;
            }).collect(Collectors.toList());
        updateBatchById(dishs);
        }*/
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.in(Dish::getId,ids);
        List<Dish> list = list(lqw);
        for (Dish dish : list) {
            if (dish.getStatus() == 1){
                dish.setStatus(0);
            } else if (dish.getStatus() == 0) {
                dish.setStatus(1);
            }
            updateById(dish);
        }

    }
}
